Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Transfer.storage_locations_transferred_to/from_in to StorageLoca… #4733

Conversation

coalest
Copy link
Collaborator

@coalest coalest commented Oct 23, 2024

(Doesn't resolve any issue)

Description

While working on something else, I noticed this Transfer class method that seemed odd in that it returns an array of StorageLocation ActiveRecord objects. Feels like it belongs more on the StorageLocation model. Moving it also simplified the method logic.

Also fixed the association's foreign_key so that ActiveRecord can join the table correctly.

Type of change

Refactor

How Has This Been Tested?

Moved model spec from Transfer to StorageLocation.

@cielf cielf requested a review from dorner October 23, 2024 16:32
Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the move. One comment about the implementation.

@@ -77,6 +77,14 @@ def self.items_inventoried(organization, inventory = nil)
end
end

def self.with_transfers_to(organization)
joins(:transfers_to).where(organization_id: organization.id).distinct.order(:name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need the where clause since the transfer has a from_id and to_id which powers the association.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the where clause, wouldn't we be grabbing every storage location that has at least one transfer from/to that location? Don't we only want the storage locations related to a certain organization?

Screenshot from 2024-10-28 13-30-41

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Storage locations belong to an organization and so do transfers. So if you have a transfer, by definition only storage locations belonging to the same organization can be involved in it.

Copy link
Collaborator Author

@coalest coalest Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I still don't understand why this where clause is unnecessary. We are only joining the transfers and storage_locations tables on the storage_locations primary key and the transfers foreign keys from_id and to_id. So it will return records for different organizations unless we specify an organization_id somewhere. (Or like if we moved the method to be an instance method on Organization so the id is specified by default.)

To illustrate in rails console in development:

  1. Organization 1 has a transfer from storage location 1 to storage location 2. Organization 2 has a transfer from storage location 3 to storage location 4.
    Screenshot from 2024-11-13 17-43-45

  2. If I remove the where clause, then I get all the storage locations that have been transferred to (locations 2 and 4), which correspond to different organizations.
    Screenshot from 2024-11-13 17-44-12

  3. When I add back in the where clause, I see only storage locations that apply to a certain organization which I thought is what we wanted.
    Screenshot from 2024-11-13 17-44-49

Am I missing something?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I was missing the fact that this was a class-level method instead of an instance-level method.

I think this could be clearer if you made these scopes instead of class-level methods.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok changed them to be scopes instead 👍

@@ -77,6 +77,14 @@ def self.items_inventoried(organization, inventory = nil)
end
end

def self.with_transfers_to(organization)
joins(:transfers_to).where(organization_id: organization.id).distinct.order(:name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I was missing the fact that this was a class-level method instead of an instance-level method.

I think this could be clearer if you made these scopes instead of class-level methods.

@coalest coalest requested a review from dorner November 28, 2024 15:18
@dorner
Copy link
Collaborator

dorner commented Nov 28, 2024

@coalest there's a conflict now :(

@cielf did you want to do a quick sanity check on the behavior here?

@coalest coalest force-pushed the Refactor-transfer-scope-to-storage-location-model branch from 053f8c5 to f2100cd Compare November 29, 2024 09:26
@coalest coalest force-pushed the Refactor-transfer-scope-to-storage-location-model branch 2 times, most recently from 16469cf to 9d20bbb Compare November 29, 2024 09:38
@coalest coalest force-pushed the Refactor-transfer-scope-to-storage-location-model branch from 9d20bbb to 1c57032 Compare November 29, 2024 09:39
@coalest
Copy link
Collaborator Author

coalest commented Nov 29, 2024

@dorner I rebased onto main and fixed the merge conflicts (keeping the same commit history). Should be good to go.

@cielf
Copy link
Collaborator

cielf commented Nov 29, 2024

Yeah, I do, but I've run out of time for today.

@cielf
Copy link
Collaborator

cielf commented Nov 30, 2024

LGTM functionally.

@dorner dorner merged commit 2ad5ba5 into rubyforgood:main Nov 30, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants